﻿package mortal.game.view.fashionOnline.view
{
    import Message.Public.*;
    import baseEngine.modifiers.*;
    import com.gengine.global.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.*;
    import mortal.common.display.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.fashionOnline.*;
    import mortal.game.view.fashionOnline.data.*;
    import mortal.game.view.fashionOnline.renderer.*;
    import mortal.game.view.fashionOnline.util.*;
    import mortal.game.view.forging.data.*;
    import mortal.mvc.core.*;

    public class FashionOnlineDisplayPanel extends FashionOnlinePanelBase
    {
        private var _bg:GBitmap;
        private var _activeTime:GTextFiled;
        private var _activeBtn:GLoadingButton;
        private var _useBtn:GLoadingButton;
        private var _combat:BitmapNumberText;
        private var _attrList:GTileList;
        private var _desc1:GTextFiled;
        private var _desc2:GTextFiled;
        private var _gotoRegistration:GButton;
        private var _fashionTagListView:FashionTagListView;
        private var _currFashionData:FashionOnlineData;
        private var _rect3d:Rect3DObject;
        private var _img2d:Img2D;
        private var _roleModelPlayer:UserModelPlayer;
        private var _3dRotationSpr:GSprite;
        private var _3dRotationSprBmp:GBitmap;
        private var _startX:Number;
        private var _endX:Number;
        private var _effectPlayer1:EffectPlayer;
        private var _effectPlayer2:EffectPlayer;

        public function FashionOnlineDisplayPanel(param1:Window)
        {
            super(param1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gBitmap("");
            this._3dRotationSpr = UICompomentPool.getUICompoment(GSprite);
            Global.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.startTurn);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.stopTurn);
            this._3dRotationSpr.mouseEnabled = true;
            this._3dRotationSpr.buttonMode = false;
            this.addChild(this._3dRotationSpr);
            this._3dRotationSprBmp = GlobalClass.getBitmap(BitmapDataConst.AlphaBMD);
            this._3dRotationSprBmp.width = 877;
            this._3dRotationSprBmp.height = 531;
            this._3dRotationSpr.addChild(this._3dRotationSprBmp);
            this._fashionTagListView = UICompomentPool.getUICompoment(FashionTagListView);
            this._fashionTagListView.x = 27;
            this._fashionTagListView.y = 14;
            this.addChild(this._fashionTagListView);
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.TextBg4, 230, 415, this));
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.size = 14;
            this._activeTime = UIFactory.gTextField("", 298, 423 - 7, 203, 30, this, _loc_1);
            this._activeBtn = UIFactory.gLoadingButton(ResFileConst.FashionOnlineActiveBtn, 332, 450, 106, 57, this);
            this._activeBtn.configEventListener(MouseEvent.CLICK, this.onActiveHandler);
            this._activeBtn.visible = false;
            this._useBtn = UIFactory.gLoadingButton(ResFileConst.UseBtn, 332, 450, 106, 57, this);
            this._useBtn.configEventListener(MouseEvent.CLICK, this.onUseHandler);
            this._useBtn.visible = false;
            this._gotoRegistration = UIFactory.gButton(Language.getString(21468), 483, 474, 70, 20, this, "GreenLinkButton");
            this._gotoRegistration.configEventListener(MouseEvent.CLICK, this.onLinkClickHandler);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.FashionOnlineAttrBg, 631, 0, this));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Text_GodEquip, 733, 13, this));
            this._combat = UIFactory.bitmapNumberText(716 + 43, 51, "WizardCombatNum.png", 23, 30, -3, this, 13);
            this._combat.align = BitmapNumberText.Mid;
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Text_GodEquipCombat, 726 + 3, 82, this));
            this.pushUIToDisposeVec(UIFactory.bg(634, 117, 242, 23, this, ImagesConst.RegionTitleBg));
            this.pushUIToDisposeVec(UIFactory.gTextField(Language.getString(21469), 653, 118, 143, 20, this, GlobalStyle.textFormatDanLan));
            this._attrList = UIFactory.tileList(653, 144, 258, 140, this);
            this._attrList.columnWidth = 128;
            this._attrList.rowHeight = 23;
            this._attrList.horizontalGap = 0;
            this._attrList.verticalGap = 0;
            this._attrList.setStyle("skin", new Bitmap());
            this._attrList.setStyle("cellRenderer", FashionOnlineAttrCellRenderer);
            this.pushUIToDisposeVec(UIFactory.bg(634, 283, 242, 23, this, ImagesConst.RegionTitleBg));
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.Text_TreasureEffect, 719, 284, this));
            _loc_1 = GlobalStyle.textFormatDanLan;
            _loc_1.indent = 23;
            this._desc1 = UIFactory.gTextField("", 653, 320, 203, 90, this, _loc_1);
            this._desc1.wordWrap = true;
            this._desc1.text = FashionOnlineUtil.getDesc1();
            this.pushUIToDisposeVec(UIFactory.bg(634, 410, 242, 2, this, ImagesConst.SplitLine));
            this._desc2 = UIFactory.gTextField("", 653, 427, 203, 80, this, _loc_1);
            this._desc2.wordWrap = true;
            this._desc2.text = FashionOnlineUtil.getDesc2();
            LoaderManager.instance.load(ImagesConst.FashionOnlineBg + ".swf", this.onLoadCompl);
            return;
        }// end function

        private function onLoadCompl(param1) : void
        {
            if (isDisposed)
            {
                return;
            }
            this._bg.bitmapData = param1.bitmapData as BitmapData;
            this.init3D();
            this.addListeners();
            this.initView();
            return;
        }// end function

        private function addListeners() : void
        {
            Dispatcher.addEventListener(EventName.FashionOnlineSwitchTag, this.onSwitchTagHandler);
            NetDispatcher.addCmdListener(ServerCommand.UpdateFashionEquip, this.onFashionUpdateHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            Dispatcher.removeEventListener(EventName.FashionOnlineSwitchTag, this.onSwitchTagHandler);
            NetDispatcher.removeCmdListener(ServerCommand.UpdateFashionEquip, this.onFashionUpdateHandler);
            return;
        }// end function

        private function init3D() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            this._rect3d = (_window as FashionOnlineModule).rect3d;
            if (this._rect3d)
            {
                this._img2d = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 877, 531), false);
                this._rect3d.addImg(this._img2d);
                this._roleModelPlayer = FrObjectPool.getObject(UserModelPlayer, this);
                _loc_1 = Cache.instance.role.entityInfo.career;
                _loc_2 = Cache.instance.role.entityInfo.sex;
                _loc_3 = Cache.instance.role.roleEntityInfo.clothes;
                _loc_4 = Cache.instance.role.roleEntityInfo.weapon;
                _loc_5 = Cache.instance.role.roleEntityInfo.hair;
                _loc_6 = Cache.instance.role.roleEntityInfo.wing;
                _loc_7 = Cache.instance.role.roleEntityInfo.entityInfo.VIP;
                this._roleModelPlayer.updateInfo(_loc_1, _loc_2, _loc_3, _loc_4, _loc_5, _loc_6, _loc_7);
                this._roleModelPlayer.scaleAll = 2.5;
                this._roleModelPlayer.rotationX = 0;
                this._roleModelPlayer.rotationY = 0;
                this._roleModelPlayer.useLight();
                this._roleModelPlayer.setRenderList(this._rect3d.renderList);
                this._rect3d.addObject3d(this._roleModelPlayer, 389, 42 + 357);
                this.add3DEffect();
            }
            return;
        }// end function

        private function remove3D() : void
        {
            if (this._rect3d)
            {
                if (this._img2d)
                {
                    this._rect3d.removeImg(this._img2d);
                    this._img2d = null;
                }
                if (this._roleModelPlayer)
                {
                    this._rect3d.disposeObj3d(this._roleModelPlayer);
                    this._roleModelPlayer = null;
                }
                this._rect3d.disposeObj3d(this._effectPlayer1);
                this._effectPlayer1 = null;
                this._rect3d.disposeObj3d(this._effectPlayer2);
                this._effectPlayer2 = null;
            }
            return;
        }// end function

        private function initView() : void
        {
            this.initFashionListTakeState();
            this.initDefaultFashionInfo();
            return;
        }// end function

        private function initFashionListTakeState() : void
        {
            this._fashionTagListView.updateFashionTakeState();
            return;
        }// end function

        private function initDefaultFashionInfo() : void
        {
            this._fashionTagListView.selFashionByType(Cache.instance.fashionOnline.getDefaultFashionType());
            return;
        }// end function

        public function updateUI() : void
        {
            this._fashionTagListView.updateFashionTakeState();
            if (this._currFashionData)
            {
                this._currFashionData.fashionState = Cache.instance.fashionOnline.getFashionStateBytype(this._currFashionData.fashionType);
            }
            this.updateBtnState();
            return;
        }// end function

        private function onSwitchTagHandler(event:DataEvent) : void
        {
            this._currFashionData = event.data as FashionOnlineData;
            if (this._currFashionData)
            {
                this.updateAll();
            }
            return;
        }// end function

        public function updateAll() : void
        {
            this.updateRoleModel();
            this.updateActiveTime();
            this.updateFashionCombat();
            this.updateReviewAttr();
            this.updateDescInfo();
            this.updateBtnState();
            return;
        }// end function

        private function updateRoleModel() : void
        {
            var _loc_1:* = FashionOnlineUtil.getRewardItemCodeById(this._currFashionData.fashionType);
            var _loc_2:* = new ItemData(_loc_1);
            var _loc_3:* = Cache.instance.role.entityInfo.career;
            var _loc_4:* = Cache.instance.role.entityInfo.sex;
            var _loc_5:* = Cache.instance.role.roleEntityInfo.clothes;
            var _loc_6:* = Cache.instance.role.roleEntityInfo.weapon;
            var _loc_7:* = Cache.instance.role.roleEntityInfo.wing;
            var _loc_8:* = Cache.instance.role.roleEntityInfo.hair;
            var _loc_9:* = _loc_2.itemInfo as ItemEquipInfo;
            if (_loc_9)
            {
                if (_loc_9.type == EEquip._EEquipFashion)
                {
                    _loc_5 = _loc_9.modelId;
                }
                if (_loc_9.type == EEquip._EEquipWing)
                {
                    _loc_7 = _loc_9.modelId;
                }
                if (_loc_9.type == EEquip._EEquipWeaponFashion && _loc_2.itemInfo.career == _loc_3)
                {
                    _loc_6 = _loc_9.modelId;
                }
                if (_loc_9.type == EEquip._EEquipHeadwear)
                {
                    _loc_8 = _loc_9.modelId;
                }
            }
            this._roleModelPlayer.updateInfo(_loc_3, _loc_4, _loc_5, _loc_6, _loc_8, _loc_7, Cache.instance.role.entityInfo.VIP);
            return;
        }// end function

        private function updateActiveTime() : void
        {
            var _loc_4:int = 0;
            var _loc_1:* = OnlineRewardConfig.instance.getInfoById(this._currFashionData.fashionType).onlineType;
            var _loc_2:* = OnlineRewardConfig.instance.getInfoById(this._currFashionData.fashionType).onlineTime;
            var _loc_3:* = Cache.instance.role.playerInfo.onlineDays;
            if (_loc_1 == FashionOnlineConst.OnlineTimeType_Hour)
            {
                this._activeTime.htmlText = Language.getStringByParam(21471, HTMLUtil.addColor(Language.getStringByParam(21472, _loc_2), GlobalStyle.colorLv));
            }
            if (_loc_1 == FashionOnlineConst.OnlineTimeType_Day)
            {
                _loc_4 = _loc_3 < _loc_2 ? (_loc_3) : (_loc_2);
                this._activeTime.htmlText = Language.getStringByParam(21473, HTMLUtil.addColor(Language.getStringByParam(21474, _loc_4, _loc_2), GlobalStyle.colorLv));
            }
            return;
        }// end function

        private function updateFashionCombat() : void
        {
            var _loc_1:* = FashionOnlineUtil.getRewardItemCodeById(this._currFashionData.fashionType);
            var _loc_2:* = new ItemData(_loc_1);
            var _loc_3:* = EquipmentUtil.getEquipCombat(_loc_2);
            this._combat.text = _loc_3 + "";
            return;
        }// end function

        private function updateReviewAttr() : void
        {
            var _loc_1:* = new DataProvider();
            _loc_1.addItems(this.getPreviewAttrData());
            this._attrList.dataProvider = _loc_1;
            this._attrList.drawNow();
            return;
        }// end function

        private function getPreviewAttrData() : Array
        {
            var _loc_5:String = null;
            var _loc_6:FashionOnlineAttrData = null;
            var _loc_1:Array = [];
            var _loc_2:int = 0;
            while (_loc_2 < (AttributeUtil.attrs.length - 1))
            {
                
                _loc_5 = AttributeUtil.attrs[_loc_2];
                _loc_6 = new FashionOnlineAttrData();
                _loc_6.attrName = _loc_5;
                _loc_6.attrValue = 0;
                _loc_1.push(_loc_6);
                _loc_2++;
            }
            var _loc_3:* = FashionOnlineUtil.getRewardItemCodeById(this._currFashionData.fashionType);
            var _loc_4:* = new ItemData(_loc_3);
            for each (_loc_6 in _loc_1)
            {
                
                _loc_6.attrValue = _loc_6.attrValue + _loc_4.itemInfo[_loc_6.attrName];
            }
            for each (_loc_6 in _loc_1)
            {
                
                _loc_6.attrName = GameDefConfig.instance.getAttributeName(_loc_6.attrName, false, 1);
            }
            return _loc_1;
        }// end function

        private function updateDescInfo() : void
        {
            return;
        }// end function

        private function updateBtnState() : void
        {
            this.clearBtnState();
            if (this._currFashionData)
            {
                switch(this._currFashionData.fashionState)
                {
                    case FashionOnlineConst.FashionState_Untake:
                    {
                        this._activeBtn.visible = true;
                        this._activeBtn.filterEnabled = false;
                        break;
                    }
                    case FashionOnlineConst.FashionState_CanTake:
                    {
                        this._activeBtn.visible = true;
                        this._activeBtn.filterEnabled = true;
                        break;
                    }
                    case FashionOnlineConst.FashionState_Taked:
                    {
                        this._useBtn.visible = true;
                        if (FashionOnlineUtil.isFashionInUse(this._currFashionData.fashionType))
                        {
                            this._useBtn.filterEnabled = false;
                        }
                        else
                        {
                            this._useBtn.filterEnabled = true;
                        }
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        private function clearBtnState() : void
        {
            this._activeBtn.visible = false;
            this._useBtn.visible = false;
            return;
        }// end function

        private function onActiveHandler(event:MouseEvent) : void
        {
            if (!this._currFashionData)
            {
                MsgManager.showRollTipsMsg(Language.getString(21470));
                return;
            }
            GameProxy.player.activeOnlineReward(this._currFashionData.fashionType);
            return;
        }// end function

        private function onUseHandler(event:MouseEvent) : void
        {
            var _loc_2:String = null;
            var _loc_3:String = null;
            var _loc_4:* = Cache.instance.pack.packRolePackCache.getEquipByType(EEquip._EEquipWeaponFashion);
            if (_loc_4)
            {
                _loc_3 = _loc_4.uid;
            }
            var _loc_5:* = FashionOnlineUtil.getFashionByType(this._currFashionData.fashionType);
            if (_loc_5)
            {
                _loc_2 = _loc_5.uid;
            }
            GameProxy.equip.dress(_loc_2, _loc_3);
            return;
        }// end function

        private function onLinkClickHandler(event:MouseEvent) : void
        {
            GameController.signUp.view.show();
            return;
        }// end function

        private function startTurn(event:MouseEvent) : void
        {
            this._startX = this.mouseX;
            this._3dRotationSpr.configEventListener(MouseEvent.MOUSE_MOVE, this.turning);
            return;
        }// end function

        private function stopTurn(event:MouseEvent) : void
        {
            this._3dRotationSpr.removeEventListener(MouseEvent.MOUSE_MOVE, this.turning);
            return;
        }// end function

        private function turning(event:MouseEvent) : void
        {
            this._endX = this.mouseX;
            if (this._roleModelPlayer)
            {
                this._roleModelPlayer.rotationY = this._roleModelPlayer.rotationY - (this._endX - this._startX) / 3;
                this._startX = this._endX;
            }
            return;
        }// end function

        private function add3DEffect() : void
        {
            if (this._rect3d)
            {
                this._rect3d.disposeObj3d(this._effectPlayer1);
                this._rect3d.disposeObj3d(this._effectPlayer2);
                this._effectPlayer1 = EffectPlayerPool.instance.getEffectPlayer(ForgingConst.EffectPath4, null, this._rect3d.renderList, false);
                this._effectPlayer1.play(PlayMode.ANIMATION_LOOP_MODE);
                this._rect3d.addObject3d(this._effectPlayer1, 180 + 141 + 81, 216 + 186);
                this._effectPlayer2 = EffectPlayerPool.instance.getEffectPlayer("CJ-shu-1", null, this._rect3d.renderList, false);
                this._effectPlayer2.play(PlayMode.ANIMATION_LOOP_MODE);
                this._rect3d.addObject3d(this._effectPlayer2, 427 - 5, 300);
            }
            return;
        }// end function

        private function onFashionUpdateHandler(param1:Object) : void
        {
            this.updateUI();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this.clearBtnState();
            this._bg.dispose(param1);
            this._fashionTagListView.dispose(param1);
            this._activeTime.dispose(param1);
            this._activeBtn.dispose(param1);
            this._combat.dispose(param1);
            this._attrList.dispose(param1);
            this._desc1.dispose(param1);
            this._desc2.dispose(param1);
            this._3dRotationSpr.dispose(param1);
            this._3dRotationSprBmp.dispose(param1);
            this._gotoRegistration.dispose(param1);
            this._useBtn.dispose(param1);
            this._bg = null;
            this._fashionTagListView = null;
            this._activeTime = null;
            this._activeBtn = null;
            this._combat = null;
            this._attrList = null;
            this._desc1 = null;
            this._desc2 = null;
            this._3dRotationSpr = null;
            this._3dRotationSprBmp = null;
            this._gotoRegistration = null;
            this._useBtn = null;
            this._currFashionData = null;
            Global.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.startTurn);
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.stopTurn);
            this.remove3D();
            this.removeListeners();
            return;
        }// end function

    }
}
